package com.nlp.trie.tst;

import java.util.LinkedList;
/**
 * 三叉字搜索树
 * @author ygsong.abcft
 *https://blog.csdn.net/shen_gang/article/details/17692769
 */

public class TSTNode {
	/**左边节点*/
	protected TSTNode loNode; 
	/**中间节点*/
	protected TSTNode eqNode;
	/**右边节点*/
	protected TSTNode hiNode;
	/**相对父节点*/
	protected TSTNode ptNode;
	
	/**  节点的值  data属性可以存储词原文和词性、词频等相关的信息  */
	public POSInf data=null;
	/**本节点表示的字符*/
	protected char splitchar;
	/**匹配失败后跳转的节点*/
	protected TSTNode _failure;
	/**存储模式串的数组变量*/
	private LinkedList<String> _results;
	
	public LinkedList<String> getResult() {
		return _results;
	}
	
	public void addResult(String result) {
		if (_results.contains(result)) {//如果已经包含该模式则不增加到模式串中
			return;
		}
		_results.add(result);
	}
	
	public String getPath() {
		StringBuilder sb = new StringBuilder();
		sb.append(this.splitchar);
		TSTNode parent = ptNode;
		while(parent != null) {
			sb.append(parent.splitchar);
			parent = parent.ptNode;
		}
		return sb.reverse().toString();
	}
	
	/*public String getPath()
	{
		StringBuilder sb = new StringBuilder();
		sb.append(this.splitchar);
		TSTNode parent = ptNode;
		TSTNode child = this;
		if(parent==null)
		{
			return String.valueOf(this.splitchar);
		}
		while (true)
		{
			//System.out.println("splitchar:"+parent.splitchar);
			if(parent.eqNode==child)
			{
				sb.append(parent.splitchar);
			}
			parent = parent.ptNode;
			child = parent;
			if(parent==null)
			{
				break;
			}
		}
		return sb.reverse().toString();
	}*/
	
	
	
	/**
	 * 构造方法
	 * @param splitchar
	 * @param parent
	 */
	public TSTNode(char splitchar, TSTNode parent) {
		this.splitchar = splitchar;
		this.ptNode = parent;
		////存储所有没有重复的模式串的数组
		_results = new LinkedList<String>();
	}

}
